<template>
  <div>
    <div class="detail-info" style="margin-top: 50px">
      <div class="form-item">
        <h4>基本信息</h4>
        <el-form :model="infoForm"  label-width="180px">
          <el-form-item label="分公司">
            <el-select v-model="infoForm.branchId" v-if="isBranch" style="width: 180px" @change="getDeptList" @visible-change="visibleChange" filterable>
              <el-option
                v-for="item in branchList"
                :label="item.branch_Name"
                :value="item.branch_Id">
              </el-option>
            </el-select>
            <el-input v-if="!isBranch" style="width: 220px" v-model="branchName" disabled></el-input>
          </el-form-item>
          <el-form-item label="所属部门">
            <el-select v-model="infoForm.deptId" v-if="isBranch" style="width: 180px" filterable>
              <el-option
                v-for="item in deptList"
                :value="item.org_id"
                :label="item.org_name">
              </el-option>
            </el-select>
            <el-input v-if="!isBranch" style="width: 220px" v-model="orgName" disabled></el-input>
          </el-form-item>
          <!--<el-form-item label="分公司">-->
            <!--<el-input v-model="this.branchName" style="width: 220px" disabled></el-input>-->
          <!--</el-form-item>-->
          <!--<el-form-item label="所属部门">-->
            <!--<el-input v-model="this.orgName" style="width: 220px" disabled></el-input>-->
          <!--</el-form-item>-->
          <el-form-item label="优惠券名称" required>
            <el-input v-model="infoForm.tname" style="width: 220px"></el-input>
          </el-form-item>
          <el-form-item label="金额" required>
            <el-input v-model="infoForm.tcost" style="width: 220px"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input  type="textarea" v-model="infoForm.remark" style="width: 350px"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div style="margin: 20px auto 80px; width: 17%">
      <el-button @click="submit" type="success">提交</el-button>
      <el-button @click="cancel" type="danger">取消</el-button>
    </div>
  </div>
</template>
<script>
  import { getInfo, activity } from '../../../../../config/proxy.js'
  import { mapState } from 'vuex'
  import mixins from '../../../../../components/mixins.vue'
  export default {
    mixins: [mixins],
    data () {
      return {
        deptList: [],
        visible: false,
        infoForm: {
          branchId: '',
          deptId: '',
          tname: '',
          tcost: '',
          remark: ''
        }
      }
    },
    methods: {
      getDeptList (val) {
        if (this.visible === true) {
          this.infoForm.deptId = ''
        }
        var _data = {}
        this.branchList.forEach(item => {
          if (val === item.branch_Id) {
            _data.orgId = item.org_id
            this.infoForm.bid = item.org_id
          }
        })
        getInfo.department(_data, (res) => {
          if (res.message === 'ok') {
            if (val === 1) {
              this.deptList = []
              res.content.forEach(element => {
                if (element.org_name.indexOf('公司') === -1) {
                  this.deptList.push(element)
                }
              })
              this.matchDept()
            } else {
              this.deptList = res.content
              this.matchDept()
            }
          } else {
            this.$message({
              showClose: true,
              message: res.message,
              type: 'error'
            })
          }
        })
      },  // 获取部门
      matchDept () { // 匹配 当前登录人的部门与部门列表
        for (let index = 0; index < this.deptList.length; index++) {
          const element = this.deptList[index]
          if (Number(this.infoForm.deptId) === Number(element.org_id)) {
            return
          } else {
            if (index === this.deptList.length - 1) {
              this.infoForm.deptId = ''
            }
          }
        }
      },
      visibleChange (val) {
        this.visible = val
      },
//    按钮组
      cancel () {
        this.$store.dispatch('delTab', {id: 'xjq_add'})
      },
      submit () {
        var regu = /^[1-9]\d*$/
        if (this.infoForm.tname === '' || this.infoForm.tcost === '') {
          this.$message({
            showClose: true,
            message: '优惠券名称和金额为必选项！',
            type: 'error'
          })
          return false
        }
        if (!regu.test(this.infoForm.tcost)) {
          this.$message({
            showClose: true,
            message: '金额不能含负，0，小数',
            type: 'error'
          })
          return false
        }
        console.log(this.infoForm)
        activity.yhqhd.jcq.xjq_creation(this.infoForm, res => {
          console.log(res)
          if (res.message === 'ok') {
            this.$message({
              showClose: true,
              message: '创建成功',
              type: 'success'
            })
            this.$store.dispatch('delTab', {id: 'xjq_add'})
            this.$store.dispatch('xjqState', this.$store.state.updateState.xjqState + 1)
          }
        })
      }
    },
    computed: mapState({
      userName: function (state) {
        return state.user.info.user_name
      },
      orgName: function (state) {
        return state.user.info.parOrgName
      },
      orgId: function (state) {
        return state.user.info.or_org_id
      },
      branchName: function (state) {
        return state.user.info.branchId === 1 ? '总公司' : state.user.info.branchName
      },
      branchId: function (state) {
        return state.user.info.branchId
      },
      branchIdOld: function (state) {
        return state.user.info.branchOrgId
      }
    }),
    created () {
      this.infoForm.branchId = this.branchId
      this.infoForm.deptId = this.orgId
      this.infoForm.bid = this.branchIdOld
      this.infoForm.createUser = this.userName
    },
    watch: {
      branchList (a, b) {
        this.getDeptList(this.infoForm.branchId)
      }
    }
  }
</script>
<style>
</style>
